package id.net.iconpln.bb.client.view;

import id.net.iconpln.bb.client.data.TestData;
import id.net.iconpln.bb.client.model.Stock;
import id.net.iconpln.bb.client.model.Waktu;
import id.net.iconpln.bb.client.service.ApprovalService;
import id.net.iconpln.bb.client.service.ApprovalServiceAsync;
import id.net.iconpln.bb.client.service.TransportRPCService;
import id.net.iconpln.bb.client.service.TransportRPCServiceAsync;
import id.net.iconpln.bb.client.util.ClientModule;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.core.FastMap;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.store.Store;
import com.extjs.gxt.ui.client.store.StoreListener;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.layout.FitData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.extjs.gxt.ui.client.widget.table.NumberCellRenderer;
import com.extjs.gxt.ui.client.widget.toolbar.LabelToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class PageBbListKonfirmKirim extends LayoutContainer {
	private final ApprovalServiceAsync service = GWT.create(ApprovalService.class);
	private FormBinding formBindings;  
	private FormData formData;
	private FormPanel panel;
	private Grid<Stock> grid;
	ListStore storePengajuan;
	ListStore storeUpdate;
	private Grid gridreal_kirim;
	ToolBar toolbar;
	
	  @Override  
	  protected void onRender(Element parent, int index) {  
	    super.onRender(parent, index);  
	    setLayout(new FitLayout());
	    storePengajuan = new ListStore();
	    storeUpdate = new ListStore();
	    formData = new FormData("-20"); 
	    ContentPanel cp = new ContentPanel();  
	  
	    cp.setHeading("Konfirmasi Pengiriman Batubara");  
	    cp.setFrame(true);  
	    cp.setSize(900, 400);  
	    cp.setLayout(new RowLayout(Orientation.HORIZONTAL));  
	  
	    grid = createGridPltu();  
	    grid.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);  
	    grid.getSelectionModel().addListener(Events.SelectionChange,  
	        new Listener<SelectionChangedEvent<Stock>>() {  
	          public void handleEvent(SelectionChangedEvent<Stock> be) {  
	            if (be.getSelection().size() > 0) {  
	              formBindings.bind((ModelData) be.getSelection().get(0));  
	            } else {  
	              formBindings.unbind();  
	            }  
	          }  
	        });  	     
	    
	    ContentPanel pan = new ContentPanel();
	    pan.setLayout(new FitLayout());
	    pan.setHeaderVisible(false);
	    pan.add(gridreal_kirim);
	    
	    Button btload = new Button("Load", new SelectionListener<ButtonEvent>() {  
		      @Override  
		      public void componentSelected(ButtonEvent ce) {
		    	 gridreal_kirim.mask("load Data ....","x-mask-loading");
		    	 storePengajuan.removeAll();
		    	 getListPengiriman();
		    	 gridreal_kirim.unmask();
		      }  
		    });  
		    
	    btload.setIcon(IconHelper.createStyle("icon-reza-refresh"));
	    
	    toolbar = new ToolBar();
	    toolbar.add(new LabelToolItem("<b>Daftar Pengajuan &nbsp;"));
	    toolbar.add(btload);
	    pan.setTopComponent(toolbar);
	  
	    FormPanel panel = createForm();  
	    formBindings = new FormBinding(panel, true);  
	    formBindings.setStore((Store) grid.getStore());  
	  
	    cp.add(panel, new RowData(.4, 1));
	    cp.add(pan, new RowData(.6, 1));
	    
	    
//		setScrollMode(Scroll.AUTO);		
	    add(cp, new FitData(5));  
	  }  
	  
	  private FormPanel createForm() {  
	    panel = new FormPanel();	    
	    panel.setHeaderVisible(false);
	    
	    FieldSet fieldSet = new FieldSet();  
	    fieldSet.setHeading("Data pengiriman");  
	    fieldSet.setCollapsible(false);
	    
	  
	    FormLayout layout = new FormLayout();  
	    layout.setLabelWidth(120);  
	    fieldSet.setLayout(layout);  

	    List<Waktu> stocks = TestData.getConfirmKirim();  
	    Collections.sort(stocks, new Comparator<Waktu>() {  
	      public int compare(Waktu arg0, Waktu arg1) {  
	        return arg0.getKode().compareTo(arg1.getNama());  
	      }  
	    });  
	    
	    ListStore<Waktu> store = new ListStore<Waktu>();  
	    store.add(stocks);  
	  
	    final TextField<String> noPengajuan = new TextField<String>();  
	    noPengajuan.setName("no_pengajuan");  
	    noPengajuan.setFieldLabel("No Pengajuan");    
	    fieldSet.add(noPengajuan, formData);
	    
	    TextField<String> noPengiriman = new TextField<String>();  
	    noPengiriman.setName("no_pengiriman");  
	    noPengiriman.setFieldLabel("No Pengiriman");    
	    fieldSet.add(noPengiriman, formData);	
	    
	    final TextField<String> noJadwal = new TextField<String>();  
	    noJadwal.setName("no_jadwal");  
	    noJadwal.setFieldLabel("No Jadwal");    
	    fieldSet.add(noJadwal, formData);

	    TextField<String> pemasok = new TextField<String>();	    
	    pemasok.setName("pemasok");  
	    pemasok.setFieldLabel("Nama Pemasok");    
	    fieldSet.add(pemasok, formData);	    	    

	    TextField<String> tonase = new TextField<String>();
	    tonase.setName("volume");  
	    tonase.setFieldLabel("Vol Kirim (Ton)");	    
	    fieldSet.add(tonase, formData);

	    TextField<String> kualitas = new TextField<String>();
	    kualitas.setName("kualitas");  
	    kualitas.setFieldLabel("Kualitas (kalori)");	    
	    fieldSet.add(kualitas, formData);
	    
	    TextField<String> status = new TextField<String>();
	    status.setValue("SHIPMENT");  
	    status.setFieldLabel("Status");
	    status.setEnabled(false);
	    fieldSet.add(status, formData);
	    	

	    panel.add(fieldSet);  	     

	    
	    FieldSet fieldSet2 = new FieldSet();  
	    fieldSet2.setHeading("ETA");  
	    fieldSet2.setCollapsible(false);
	    
	    FormLayout layout2 = new FormLayout();  
	    layout2.setLabelWidth(100);  
	    fieldSet2.setLayout(layout2);  	

	    
	    TextField<String> pltutj = new TextField<String>();	    
	    pltutj.setName("nama_pembangkit");  
	    pltutj.setFieldLabel("PLTU tujuan");    
	    fieldSet2.add(pltutj, formData);

	    
	    TextField<String> berangkat = new TextField<String>();
//	    DateField berangkat = new DateField();
	    berangkat.setName("tanggal_eta");  
	    berangkat.setFieldLabel("Estimasi Tgl kirim");    
	    fieldSet2.add(berangkat, formData);	    	    	    
	    
	    TextField<String> tiba = new TextField<String>();
//	    DateField tiba = new DateField();
	    tiba.setName("tanggal_etd");  
	    tiba.setFieldLabel("Estimasi Tgl tiba");    
	    fieldSet2.add(tiba, formData);
	    
	    panel.add(fieldSet2);
	    
	    
	    FieldSet fieldSet3 = new FieldSet();  
	    fieldSet3.setHeading("Data Transportasi");  
	    fieldSet3.setCollapsible(false);

	    FormLayout layout3 = new FormLayout();  
	    layout3.setLabelWidth(160);  
	    fieldSet3.setLayout(layout3);  

	    	    	    	   
	    TextField<String> namakpl = new TextField<String>();
	    namakpl.setName("nama_kapal");  
	    namakpl.setFieldLabel("Nama Kapal");	    
	    fieldSet3.add(namakpl, formData);
	    
	    TextField<String> nogps = new TextField<String>();
	    nogps.setName("gps_no");  
	    nogps.setFieldLabel("No GPS");	    
	    fieldSet3.add(nogps, formData);
	    
	    TextField<String> provider = new TextField<String>();
	    provider.setName("provider");  
	    provider.setFieldLabel("Provider");	    
	    fieldSet3.add(provider, formData);
	    
	    panel.add(fieldSet3);

	    ContentPanel p = new ContentPanel();
	    p.setHeaderVisible(false);	    
	    p.setButtonAlign(HorizontalAlignment.RIGHT);  
	    
	    p.addButton(new Button("Submit", new SelectionListener<ButtonEvent>() {  
		      @Override  
		      public void componentSelected(ButtonEvent ce) {  
		    	    String session = Registry.get("isModule");  	
		    	  	Map mapParam = new FastMap();
		    	  	mapParam.put("no_pengajuan", noPengajuan.getValue());
		    	  	mapParam.put("no_jadwal", noJadwal.getValue());
		    		mapParam.put("user", "REZA");
		    		mapParam.put("app_access", "WEB");
		    		mapParam.put("crud", "UPDATE_APPROVAL");
		    		

		    		service.insertBbPengirimanBb("PENGAJUAN", mapParam,new AsyncCallback<List<Map>>() {
		    			public void onFailure(Throwable caught) {
		    				GWT.log(caught.getMessage(), null);
		    			}

		    			public void onSuccess(List<Map> result) {
		    				System.out.println("masuk on sucses");
		    				gridreal_kirim.mask("load Data ....","x-mask-loading");
		    				storePengajuan.removeAll();
		    				getListPengiriman();
		    				gridreal_kirim.unmask();
		    			}
		    		});
		    		gridreal_kirim.getStore().commitChanges();
		    	  
		        
		        
		      }  
		    }));  

	    panel.add(p);
	    
	    p.addButton(new Button("Reset", new SelectionListener<ButtonEvent>() {  
	      @Override  
	      public void componentSelected(ButtonEvent ce) {  
	        grid.getStore().rejectChanges();
	        panel.clear();
	      }  
	    }));  
	    
	    panel.setScrollMode(Scroll.AUTO);
	    
	    return panel;  
	  }  
	  
	  
	  
	  
	  private Grid createGridPltu(){
		    List<ColumnConfig> configs = new ArrayList<ColumnConfig>();  
		    
		    
		    getListPengiriman();
		    
		    ColumnConfig column = new ColumnConfig(); 
		    
		    column = new ColumnConfig("no_pengiriman", "No Pengajuan", 150);  
		    configs.add(column); 
		    
		    column = new ColumnConfig("pemasok", "Pemasok", 150);  
		    configs.add(column);
		    
		    column = new ColumnConfig("volume", "Volume", 150);  
		    configs.add(column);
		    
		    column = new ColumnConfig("status", "Status", 150);  
		    configs.add(column);
		    
		    column = new ColumnConfig("nama_pembangkit", "Pembangkit", 150);  
		    configs.add(column);
		    
		    column = new ColumnConfig("tanggal_eta", "Estimasi TA", 150);  
		    configs.add(column);
		    
		    column = new ColumnConfig("tanggal_etd", "Estimasi  TD", 150);  
		    configs.add(column);
		    
		    column = new ColumnConfig("nama_transportir", "Transportir", 150);  
		    configs.add(column);
		    
		    column = new ColumnConfig("gps_no", "No GPS", 150);  
		    configs.add(column);
		    
		    column = new ColumnConfig("provider", "Provider", 150);  
		    configs.add(column);
		    
		    
		    storePengajuan.setMonitorChanges(true); 
		    ColumnModel cm = new ColumnModel(configs);  
		  
		    gridreal_kirim = new Grid(storePengajuan, cm);  
		    gridreal_kirim.getView().setEmptyText("Data Empty");  
		    gridreal_kirim.setBorders(true);  
		  
		    return gridreal_kirim;  
	  }   
	  
	  private void getListPengiriman(){
		  Map mapParam = new FastMap();
  		  mapParam.put("id_pltu", "2");
  		  
  		  	service.getlistpengiriman_approval("PENGAJUAN_APPROVAL", mapParam,new AsyncCallback<List<Map>>() {
  			public void onFailure(Throwable caught) {
  				GWT.log(caught.getMessage(), null);
  			}

  			public void onSuccess(List<Map> result) {	
  				storePengajuan.removeAll();
  				storePengajuan.add(ClientModule.setListBaseModelFromListMap(result));
  			}
  		  });
	  }
	  
}
